package com.kfc.order.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.SendResult;
import com.kfc.order.service.CustomerManager;
import com.kfc.order.vo.Customer;


@Controller
public class CustomerController {

	@Autowired(required = true)
	private CustomerManager cm;
	private String ip;
	private String name ;
	private String time ;
	// ��¼
	@RequestMapping("login")
	public String login(String customertel, String customerpsd,
			HttpServletRequest request) throws Exception{
		Customer customer = cm.login(customertel, customerpsd);
		name = customer.getCustomertel();
		System.out.println(name);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		time = df.format(new Date());
		System.out.println(time);// 
		
		ip = request.getRemoteAddr();
		System.out.println(ip);
		JSONObject jso = new JSONObject();
		jso.put("name", name);
		jso.put("time", time);
		jso.put("ip", ip);
		String s = jso.toString();
		
		
		Properties properties = new Properties();
	       properties.put(PropertyKeyConst.ProducerId, "PID_tom");
	       properties.put(PropertyKeyConst.AccessKey, "MMYLKTrsU2nlgQ9d");
	       properties.put(PropertyKeyConst.SecretKey, "iOMjB2w5fHngqxsiHGj1n0mWngi2iO");
	       Producer producer = ONSFactory.createProducer(properties);
	           
	       //在发送消息前，必须调用start方法来启动Producer，只需调用一次即可。
	       producer.start();
	       Message msg = new Message(
	            //Message Topic
	            "Demo001", 
	            //Message Tag,
	            //可理解为Gmail中的标签，对消息进行再归类，方便Consumer指定过滤条件在ONS服务器过滤      

	  
	            "TagB",
	            //Message Body
	            //任何二进制形式的数据，ONS不做任何干预，需要Producer与Consumer协商好一致的序列化和反序列化方式
	          
	            
	            
	            s.getBytes()
	        );
	        
	        // 设置代表消息的业务关键属性，请尽可能全局唯一。
	        // 以方便您在无法正常收到消息情况下，可通过ONS Console查询消息并补发。
	        // 注意：不设置也不会影响消息正常收发
	        msg.setKey("ORDERID_100");
	        
	        //发送消息，只要不抛异常就是成功
	        SendResult sendResult = producer.send(msg);
	        System.out.println(sendResult);

	        // 在应用退出前，销毁Producer对象
	        // 注意：如果不销毁也没有问题
	        producer.shutdown();
		System.out.println(customertel +"==================="+ customerpsd);
		if (customer != null) {
			request.getSession().setAttribute("customer", customer);
			return "success";
		} else {
			return "login";
		}
	}

	
	
}
